package org.hgvc.webapp.ets.jsf.beans.agent;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Complaint;
import org.hgvc.webapp.ets.constant.ComplaintConstant;
import org.hgvc.webapp.ets.spring.service.ComplaintService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AgentComplaintBean extends AbstractBean {

	public static final String BEAN_NAME="agentComplaintBean";
	public static final String NAV_LIST="agentListComplaint";
	public static final String NAV_CREATE="agentCreateComplaint";
	public static final String NAV_READ="agentReadComplaint";
	public static final String NAV_VIEW="agentViewComplaint";
	private Complaint complaint;
	private ComplaintService service;
	private List<Complaint> complaintList;
	private Agent agent;
	private String status = ComplaintConstant.COMPLAINT_STATUS_ACK;
	
	public Complaint getComplaint() {
		return complaint;
	}
	public void setComplaint(Complaint complaint) {
		this.complaint = complaint;
	}
	
	public void setService(ComplaintService service) {
		this.service = service;
	}
	
	/*--------------------------------------------------------------------------------------------*/
	
	public String getStatus() {
		return status;
	}
	public List<Complaint> getComplaintList() {
		try{
			if(agent==null){
				AgentBean bean = (AgentBean)FacesUtils.getManagedBean(AgentBean.BEAN_NAME);
				agent = bean.getAgent();
			}
			complaintList = service.getComplaintListByRefIdType(agent.getAgentId(), ComplaintConstant.COMPLAINT_TYPE_AGENT);
		}catch (BaseException be) {
			be.printStackTrace();
			initErrorBean(be);
		}
		return complaintList;
	}
	
	public String navListAction(){
		return NAV_LIST;
	}
	
	public String navCreateAction(){
		complaint = new Complaint();
		complaint.setType(ComplaintConstant.COMPLAINT_TYPE_AGENT);
		complaint.setRefId(agent.getAgentId());
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			service.createComplaint(complaint, getThisUser());
			return NAV_LIST;
		}catch (BaseException be) {
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_CREATE;
	}
	
	public void viewActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("vcompId");
		String id = (String)param.getValue();
		for(Complaint comp:complaintList){
			if(comp.getComplaintId().equalsIgnoreCase(id)){
				complaint = comp;
				break;
			}
		}
	}
	
	public String navViewAction(){
		return NAV_VIEW;
	}
	
	public void readActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("compId");
		String id = (String)param.getValue();
		for(Complaint comp:complaintList){
			if(comp.getComplaintId().equalsIgnoreCase(id)){
				complaint = comp;
				break;
			}
		}
	}
	
	
	public String navReadAction(){
		return NAV_READ;
	}
	
	public String navBackAction(){
		return AgentBean.NAV_PORTAL;
	}
}
